var Job = {
    id: "jobTable",
    seItem: null,
    table: null,
    layerIndex: -1,
}

Job.defaultColumn = function () {
    return [
        {
            checkbox: true
        },
        {
            field: 'jobId',
            title: '任务ID'
        }, {
            field: 'beanName',
            title: 'Bean名称'
        }, {
            field: 'methodName',
            title: '方法名称'
        }, {
            field: 'params',
            title: '参数'
        }, {
            field: 'cronExpression',
            title: 'cron表达式'
        }, {
            field: 'remark',
            title: '备注'
        }, {
            field: 'status',
            title: '状态',
            formatter: function (value, row, index) {
                if (value === '1') return '<span class="badge badge-danger">暂停</span>';
                if (value === '0') return '<span class="badge badge-success">正常</span>';
            }
        }
    ]
}

/**
 * 检查是否选中
 */
Job.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        MgrUser.seItem = selected[0];
        return true;
    }
};

/**
 * 新增任务
 */
Job.addJob = function () {
    var index = layer.open({
        type: 2,
        title: '新增任务信息',
        area: ['800px', '500px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + ctxPath + 'job/job_add/'
    });

    this.layerIndex = index;
}

/**
 * 删除任务
 */
Job.delJob = function () {
    if (this.check()) {
        var operation = function(){
            var userId = MgrUser.seItem.id;
            var ajax = new $ax(Feng.ctxPath + "/mgr/delete", function () {
                Feng.success("删除成功!");
                MgrUser.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("userId", userId);
            ajax.start();
        };

        Feng.confirm("是否删除用户" + MgrUser.seItem.account + "?",operation);
    }
}

/**
 * 修改任务
 */
Job.updateJob = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '编辑管理员',
            area: ['800px', '450px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/mgr/user_edit/' + this.seItem.id
        });
        this.layerIndex = index;
    }
}

/**
 * 执行任务
 */
Job.runJob = function () {
    var operation = function(){
        var selected = $("#jobTable").bootstrapTable('getSelections');
        var selected_length = selected.length;
        if (!selected_length) {
            Feng.info("请至少选中表格中的某一记录！");
            return;
        }
        var ids = "";
        for (var i = 0; i < selected_length; i++) {
            ids += selected[i].jobId;
            if (i !== (selected_length - 1)) ids += ",";
        }

        var ajax = new $ax(Feng.ctxPath + "/job/run", function () {
            Feng.success("执行成功!");
            // Job.table.refresh();
            $("#"+Job.id).bootstrapTable("refresh");
        }, function (data) {
            Feng.error("执行失败!" + data.responseJSON.message + "!");
        });
        ajax.set("jobIds", ids);
        ajax.start();
    };

    Feng.confirm("确定执行选中的任务 ？" ,operation);
}


/**
 * 暂停任务
 */
Job.pauseJob = function () {
    var operation = function(){
        var selected = $("#" + Job.id).bootstrapTable('getSelections');
        var selected_length = selected.length;
        if (!selected_length) {
            Feng.info("请至少选中表格中的某一记录！");
            return;
        }
        var ids = "";
        for (var i = 0; i < selected_length; i++) {
            if (selected[i].status == '1'){
                Feng.alert("请选择已执行的任务！");
                return;
            }

            ids += selected[i].jobId;
            if (i !== (selected_length - 1)) ids += ",";
        }

        var ajax = new $ax(Feng.ctxPath + "/job/pause", function () {
            Feng.success("暂停成功!");
            $("#"+Job.id).bootstrapTable("refresh");
        }, function (data) {
            Feng.alert("暂停失败!" + data.responseJSON.message + "!");
        });
        ajax.set("jobIds", ids);
        ajax.start();
    };

    Feng.confirm("确定执行选中的任务 ？" ,operation);
}

/**
 * 恢复任务
 */
Job.resumeJob = function () {

}


Job.search = function () {
    var params = {};

    params['name'] = $("#name").val();
    params['methodName'] = $("#methodName").val();
    Job.table.refresh({query: params});
}

$(function () {
    var bsTable = new BSTable(Job.id, ctxPath + "job/list", Job.defaultColumn())
    bsTable.setPaginationType("client");
    this.table = bsTable.init();
})